//
//  MyCarViewController.m
//  Logistics
//
//  Created by hy002 on 2018/1/19.
//  Copyright © 2018年 HUYU. All rights reserved.
//

#import "MyCarViewController.h"
#import <AVFoundation/AVCaptureDevice.h>
#import <AVFoundation/AVMediaFormat.h>
#import "TruckTypeModel.h"
#import "LengthModel.h"
#import "CloseGoodsAddressViewController.h"
#import "LogisticsLocationViewController.h"
#import "CarDetailModel.h"
static NSString *cellPath = @"MyCarCell";
@interface MyCarViewController ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate,UIActionSheetDelegate,UIScrollViewDelegate,UIPickerViewDelegate,UIPickerViewDataSource>
{
    UIPickerView *pickTypeView;
}
@property (nonatomic, strong)UITableView *tableview;
@property (nonatomic,strong)UIView *TypeView;
@property (nonatomic, strong)NSArray *titAry;
@property (nonatomic, strong)NSArray *allTypeAry;
@property (nonatomic, strong)NSString *carTypeStr;
@property (nonatomic, strong)NSString *carNumStr;
@property (nonatomic, strong)NSString *carLenthStr;
@property (nonatomic, strong)NSString *carWidthStr;
@property (nonatomic, strong)NSString *carHeightStr;
@property (nonatomic, strong)NSString *carWeightStr;
@property (nonatomic, strong)NSString *carPlaceStr;
@property (nonatomic, strong)NSString *carCoordinStr;
@property (nonatomic, strong)NSString *DriverImgUrl;
@property (nonatomic, strong)NSString *VehicleImgUrl;
@property (nonatomic, strong)NSString *HeadImgUrl;
@property (nonatomic, strong)NSString *FootImgUrl;
@property (nonatomic, strong)NSString *BodyImgUrl;
@property (nonatomic, strong)NSString *latitude;
@property (nonatomic, strong)NSString *lontude;
@property (nonatomic, strong)NSString *streeID;
@property (nonatomic, strong)NSString *carTypeID;
@property (nonatomic, strong)CarDetailModel *carModel;

@property (nonatomic, assign)NSInteger imgType;
@property (nonatomic, assign)NSInteger TruckType;
@property (nonatomic, assign)NSInteger lenthType;

@property (nonatomic, assign)BOOL isLenth;

@end

@implementation MyCarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _titAry = @[@"货车类型",@"车牌号码",@"车辆长度",@"",@"停车地点",@"定位坐标"];
    [self setAllName];
    [self creatUI];
    [self requestTruckType];
    [self requestTruckVerifyInfo];
}

- (void)setAllName
{
    _imgType = 0;
    _TruckType = 0;
    _lenthType = 0;
    _carTypeStr = @"";
    _carNumStr = @"";
    _carLenthStr = @"";
    _carWidthStr = @"";
    _carHeightStr = @"";
    _carWeightStr = @"";
    _carPlaceStr = @"";
    _carCoordinStr = @"";
    _DriverImgUrl = @"";
    _VehicleImgUrl = @"";
    _HeadImgUrl = @"";
    _FootImgUrl = @"";
    _BodyImgUrl = @"";
    _streeID = @"";
    _latitude = @"";
    _lontude = @"";
    _carTypeID = @"";
}

- (void)creatUI
{
    self.navigationItem.title = @"货车登记";
    self.view.backgroundColor = BacksColor;
    self.navigationController.navigationBar.translucent = NO;
    self.tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-STA_NAV_HEIGHT) style:UITableViewStylePlain];
    _tableview.backgroundColor = BacksColor;
    _tableview.delegate = self;
    _tableview.dataSource = self;
    _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableview];
}

- (void)companyAction
{
    if (!self.TypeView)
    {
        self.TypeView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-STA_NAV_HEIGHT)];
        _TypeView.backgroundColor = RGBA(0, 0, 0, 0.6);
        [self.view addSubview:_TypeView];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(bottomViewPressed)];
        [_TypeView addGestureRecognizer:tap];
        pickTypeView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT-250*displayheight-STA_NAV_HEIGHT, SCREEN_WIDTH, 250*displayheight)];
        pickTypeView.backgroundColor = [UIColor whiteColor];
        pickTypeView.delegate = self;
        pickTypeView.dataSource = self;
        [_TypeView addSubview:pickTypeView];
        UIView *whiteView = [[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT-280*displayheight-STA_NAV_HEIGHT, SCREEN_WIDTH, 30*displayheight)];
        whiteView.backgroundColor = [UIColor whiteColor];
        [_TypeView addSubview:whiteView];
        UIButton *cancelBtn = [ZCControl createButtonWithFrame:CGRectMake(15*displaywidth, 10*displayheight, 40*displaywidth, 20*displayheight) ImageName:nil Target:self Action:@selector(bottomViewPressed) Title:@"取消" Font:13.0];
        cancelBtn.backgroundColor = ThemeColor;
        [cancelBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        cancelBtn.layer.cornerRadius = 3;
        cancelBtn.layer.masksToBounds = YES;
        [whiteView addSubview:cancelBtn];
        
        UIButton *soureBtn = [ZCControl createButtonWithFrame:CGRectMake(SCREEN_WIDTH- 55*displaywidth, 10*displayheight, 40*displaywidth, 20*displayheight) ImageName:nil Target:self Action:@selector(soureAction) Title:@"确定" Font:13.0];
        soureBtn.backgroundColor = ThemeColor;
        [soureBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        soureBtn.layer.cornerRadius = 3;
        soureBtn.layer.masksToBounds = YES;
        [whiteView addSubview:soureBtn];
        
    }
    _TypeView.hidden = NO;
    
}

- (void)soureAction
{
    if (_isLenth)
    {
        NSInteger selectRow = [pickTypeView selectedRowInComponent:0];
        TruckTypeModel *model = _allTypeAry[_TruckType-1];
        NSArray *lenthAry = model.truckLengths;
        LengthModel *lenModel = lenthAry[selectRow];
        _carLenthStr = lenModel.length;
        _lenthType = selectRow+1;
        
    }
    else
    {
        NSInteger selectRow = [pickTypeView selectedRowInComponent:0];
        TruckTypeModel *model = _allTypeAry[selectRow];
        _carTypeStr = model.name;
        _carTypeID = s_str(model.ID);
        _TruckType = selectRow+1;
        
    }
    [self bottomViewPressed];
    [_tableview reloadData];
}


- (void)bottomViewPressed
{
    _TypeView.hidden = YES;
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

//指定每个表盘上有几行数据
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (_isLenth)
    {
        if (_TruckType != 0)
        {
            TruckTypeModel *model = _allTypeAry[_TruckType-1];
            NSArray *lenthAry = model.truckLengths;
            return lenthAry.count;
        }
        return 0;
    }
    return _allTypeAry.count;
}

#pragma mark UIPickerView Delegate Method 代理方法

//指定每行如何展示数据（此处和tableview类似）
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if (_isLenth)
    {
        TruckTypeModel *model = _allTypeAry[_TruckType-1];
        NSArray *lenthAry = model.truckLengths;
        LengthModel *lenModel = lenthAry[row];
        return lenModel.length;
    }
    else
    {
        TruckTypeModel *model = _allTypeAry[row];
        return model.name;
    }
    
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    NSLog(@"%ld------%ld",component,row);
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 4;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0)
    {
        return _titAry.count;
    }
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellPath];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = BacksColor;
    UIView *whiteView = [[UIView alloc] initWithFrame:CGRectMake(0, 10*displaywidth, SCREEN_WIDTH, 45*displaywidth)];
    whiteView.backgroundColor = [UIColor whiteColor];
    [cell.contentView addSubview:whiteView];
    if (indexPath.section == 0)
    {
        if (indexPath.row != 3)
        {
            UILabel *titLab = [ZCControl createLabelWithFrame:CGRectMake(0, 5*displaywidth, 95*displaywidth, 35*displaywidth) Font:15.0 Text:_titAry[indexPath.row] Color:BlaTitColor];
            titLab.textAlignment = NSTextAlignmentRight;
            [whiteView addSubview:titLab];
            UIView *line1 = [[UIView alloc] initWithFrame:CGRectMake(100*displaywidth, 12.5*displaywidth, 1, 20*displaywidth)];
            line1.backgroundColor = RGB(204, 204, 204);
            [whiteView addSubview:line1];
            if (indexPath.row == 1)
            {
                UITextField *textfield = [ZCControl createTextFieldWithFrame:CGRectMake(110*displaywidth, 2.5*displaywidth, SCREEN_WIDTH-125*displaywidth, 40*displaywidth) placeholder:@"" passWord:NO leftImageView:nil rightImageView:nil Font:13.0];
                [textfield setValue:RGB(135, 135, 135) forKeyPath:@"_placeholderLabel.textColor"];
                textfield.delegate = self;
                textfield.clearButtonMode = UITextFieldViewModeWhileEditing;
                textfield.textColor = BlaTitColor;
                textfield.textAlignment = NSTextAlignmentLeft;
                textfield.text = _carNumStr;
                textfield.tag = 201;
                [whiteView addSubview:textfield];
            }
            else
            {
                UIImageView *rightImg = [ZCControl createImageViewWithFrame:CGRectMake(SCREEN_WIDTH-30*displaywidth, 12.5*displayheight, 15*displaywidth, 20*displayheight) ImageName:@"gs_icon05"];
                rightImg.contentMode = UIViewContentModeScaleAspectFit;
                [whiteView addSubview:rightImg];
                NSArray *nameAry = @[_carTypeStr,@"",_carLenthStr,@"",_carPlaceStr,_carCoordinStr];
                UILabel *nameLab = [ZCControl createLabelWithFrame:CGRectMake(110*displaywidth, 0, SCREEN_WIDTH-180*displaywidth, 45*displaywidth) Font:14.0 Text:nameAry[indexPath.row] Color:BlaTitColor];
                [whiteView addSubview:nameLab];
                if (indexPath.row == 2)
                {
                    UILabel *unitLab = [ZCControl createLabelWithFrame:CGRectMake(SCREEN_WIDTH-70*displaywidth, 5*displaywidth, 35*displaywidth, 35*displaywidth) Font:13.0 Text:@"/米" Color:RGB(180, 180, 180)];
                    unitLab.textAlignment = NSTextAlignmentRight;
                    [whiteView addSubview:unitLab];
                }
            }
        }
        else
        {
            NSArray *titleArray = @[@"宽",@"高",@"载重"];
            NSArray *unitAry = @[@"/米",@"/米",@"/吨"];
            NSArray *textArray = @[_carWidthStr,_carHeightStr,_carWeightStr];
            CGFloat W = (SCREEN_WIDTH-15)/3;
            for (int i = 0; i < titleArray.count; i++)
            {
                UILabel *titLabel = [ZCControl createLabelWithFrame:CGRectMake(W*i, 5*displaywidth, 35*displaywidth, 35*displaywidth) Font:14.0 Text:titleArray[i] Color:BlaTitColor];
                titLabel.textAlignment = NSTextAlignmentRight;
                [whiteView addSubview:titLabel];
                UIView *kuangView = [[UIView alloc] initWithFrame:CGRectMake(40*displaywidth+W*i, 5*displaywidth, W-40*displaywidth, 35*displaywidth)];
                kuangView.backgroundColor = [UIColor whiteColor];
                kuangView.layer.cornerRadius = 2;
                kuangView.layer.masksToBounds = YES;
                kuangView.layer.borderWidth = 1;
                kuangView.layer.borderColor = BacksColor.CGColor;
                [whiteView addSubview:kuangView];
                UILabel *unitLabel = [ZCControl createLabelWithFrame:CGRectMake(W-60*displaywidth, 0, 20*displaywidth, 35*displaywidth) Font:11.0 Text:unitAry[i] Color:RGB(180, 180, 180)];
                unitLabel.textAlignment = NSTextAlignmentCenter;
                [kuangView addSubview:unitLabel];
                UITextField *textfield = [ZCControl createTextFieldWithFrame:CGRectMake(5*displaywidth, 0, W-65*displaywidth, 35*displaywidth) placeholder:@"" passWord:NO leftImageView:nil rightImageView:nil Font:14.0];
                [textfield setValue:RGB(135, 135, 135) forKeyPath:@"_placeholderLabel.textColor"];
                textfield.delegate = self;
                textfield.clearButtonMode = UITextFieldViewModeWhileEditing;
                textfield.keyboardType = UIKeyboardTypeDecimalPad;
                textfield.textColor = BlaTitColor;
                textfield.textAlignment = NSTextAlignmentLeft;
                textfield.text = textArray[i];
                textfield.tag = 230+i;
                [kuangView addSubview:textfield];
                
            }
        }
       
    }
    else if (indexPath.section == 1)
    {
        whiteView.frame = CGRectMake(0, 10*displaywidth, SCREEN_WIDTH, 170*displaywidth);
        UILabel *titLab = [ZCControl createLabelWithFrame:CGRectMake(15*displaywidth, 10*displaywidth, 150*displaywidth, 30*displaywidth) Font:15.0 Text:@"车辆证件" Color:BlaTitColor];
        [whiteView addSubview:titLab];
        NSArray *imgtitAry = @[@"上传驾驶证",@"上传行驶证"];
        CGFloat width = (SCREEN_WIDTH-45*displaywidth)/2;
        NSArray *imgUrlAry = @[_DriverImgUrl,_VehicleImgUrl];
        for (int i = 0; i < imgtitAry.count; i++)
        {
            UIView *backV = [[UIView alloc] initWithFrame:CGRectMake(15*displaywidth+(width+15*displaywidth)*i, 55*displaywidth, width, 100*displaywidth)];
            backV.backgroundColor = RGB(244, 241, 244);
            backV.layer.cornerRadius = 5;
            backV.layer.masksToBounds = YES;
            [whiteView addSubview:backV];
            UIImageView *imgV = [ZCControl createImageViewWithFrame:CGRectMake(width/2-22.5*displaywidth, 22.5*displaywidth, 45*displaywidth, 45*displaywidth) ImageName:@"my_icon07"];
            imgV.contentMode = UIViewContentModeScaleAspectFit;
            [backV addSubview:imgV];
            UILabel *titlab = [ZCControl createLabelWithFrame:CGRectMake(0, 70*displaywidth, width, 30*displaywidth) Font:12.0 Text:imgtitAry[i] Color:RGB(32, 69, 139)];
            titlab.textAlignment = NSTextAlignmentCenter;
            [backV addSubview:titlab];
            UIImageView *cardImgview = [ZCControl createImageViewWithFrame:CGRectMake(0, 0, width, 100*displaywidth) ImageName:nil];
            cardImgview.backgroundColor = [UIColor clearColor];
            if (![imgUrlAry[i] isEqualToString:@""])
            {
                [cardImgview sd_setImageWithURL:URL(imgUrlAry[i])];
            }
            cardImgview.contentMode = UIViewContentModeScaleAspectFill;
            cardImgview.layer.cornerRadius = 5;
            cardImgview.layer.masksToBounds = YES;
            [backV addSubview:cardImgview];
            UIButton *btn = [ZCControl createButtonWithFrame:CGRectMake(0, 0, width, 100*displayheight) ImageName:nil Target:self Action:@selector(pushAction:) Title:nil Font:12.0];
            btn.backgroundColor = [UIColor clearColor];
            btn.tag = 300+i;
            [cardImgview addSubview:btn];
        }
    }
    else if (indexPath.section == 2)
    {
        whiteView.frame = CGRectMake(0, 10*displaywidth, SCREEN_WIDTH, 140*displaywidth);
        UILabel *titLab = [ZCControl createLabelWithFrame:CGRectMake(15*displaywidth, 10*displaywidth, 150*displaywidth, 30*displaywidth) Font:15.0 Text:@"我的货车照" Color:BlaTitColor];
        [whiteView addSubview:titLab];
        NSArray *imgtitAry = @[@"上传车头照",@"上传车身照",@"手持车尾照"];
        NSArray *urlAry = @[_HeadImgUrl,_BodyImgUrl,_FootImgUrl];
        CGFloat width = (SCREEN_WIDTH-60*displaywidth)/3;
        for (int i = 0; i < imgtitAry.count; i++)
        {
            UIView *backV = [[UIView alloc] initWithFrame:CGRectMake(15*displaywidth+(width+15*displaywidth)*i, 55*displaywidth, width, 70*displaywidth)];
            backV.backgroundColor = RGB(245, 245, 245);
            backV.layer.cornerRadius = 5;
            backV.layer.masksToBounds = YES;
            [whiteView addSubview:backV];
            UIImageView *imgV = [ZCControl createImageViewWithFrame:CGRectMake(width/2-15*displaywidth, 10*displaywidth, 30*displaywidth, 30*displaywidth) ImageName:@"my_icon07"];
            imgV.contentMode = UIViewContentModeScaleAspectFit;
            [backV addSubview:imgV];
            UILabel *titlab = [ZCControl createLabelWithFrame:CGRectMake(0, 40*displaywidth, width, 30*displaywidth) Font:12.0 Text:imgtitAry[i] Color:RGB(32, 69, 139)];
            titlab.textAlignment = NSTextAlignmentCenter;
            [backV addSubview:titlab];
            UIImageView *cardImgview = [ZCControl createImageViewWithFrame:CGRectMake(0, 0, width, 70*displaywidth) ImageName:nil];
            cardImgview.backgroundColor = [UIColor clearColor];
            if (![urlAry[i] isEqualToString:@""])
            {
                [cardImgview sd_setImageWithURL:URL(urlAry[i])];
            }
            cardImgview.contentMode = UIViewContentModeScaleAspectFill;
            cardImgview.layer.cornerRadius = 5;
            cardImgview.layer.masksToBounds = YES;
            [backV addSubview:cardImgview];
            UIButton *btn = [ZCControl createButtonWithFrame:CGRectMake(0, 0, width, 70*displayheight) ImageName:nil Target:self Action:@selector(pushAction:) Title:nil Font:12.0];
            btn.backgroundColor = [UIColor clearColor];
            btn.tag = 400+i;
            [cardImgview addSubview:btn];
        }
   
        
    }
    else
    {
        whiteView.hidden = YES;
        UIButton *cancelBtn = [ZCControl createButtonWithFrame:CGRectMake(15*displaywidth, 30*displaywidth, SCREEN_WIDTH/2-22.5*displaywidth, 45*displayheight) ImageName:nil Target:self Action:@selector(cancelAction) Title:@"取消" Font:16.0];
        cancelBtn.backgroundColor = RGB(204, 204, 204);
        [cancelBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        cancelBtn.layer.cornerRadius = 5;
        cancelBtn.layer.masksToBounds = YES;
        [cell.contentView addSubview:cancelBtn];
        
        UIButton *saveBtn = [ZCControl createButtonWithFrame:CGRectMake(SCREEN_WIDTH/2+7.5*displaywidth, 30*displaywidth, SCREEN_WIDTH/2-22.5*displaywidth, 45*displayheight) ImageName:nil Target:self Action:@selector(saveBtnAction) Title:@"提交货车信息" Font:16.0];
        saveBtn.backgroundColor = RGB(255, 153, 0);
        [saveBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        saveBtn.layer.cornerRadius = 5;
        saveBtn.layer.masksToBounds = YES;
        [cell.contentView addSubview:saveBtn];
    }
        
    
    
    return cell;
}

- (void)cancelAction
{
    [self.navigationController popViewControllerAnimated:YES];
}

//提交货车信息
- (void)saveBtnAction
{
    [self.view endEditing:YES];
    NSArray *nameARRAY = @[_carTypeStr,_carNumStr,_carLenthStr,_carWidthStr,_carHeightStr,_carWeightStr,_carPlaceStr,_carCoordinStr,_DriverImgUrl,_VehicleImgUrl,_HeadImgUrl,_BodyImgUrl,_FootImgUrl];
    NSArray *tostAry = @[@"请选择货车类型",@"请输入车牌号",@"请选择车辆长度",@"请输入车宽",@"请输入车高",@"请输入车辆载重",@"请选择停车地点",@"请选择定位坐标",@"请上传驾驶证",@"请上传行驶证",@"请上传车头照",@"请上传车身照",@"请上传车尾照",@""];
    for (int i = 0; i < nameARRAY.count; i++)
    {
        if ([nameARRAY[i] isEqualToString:@""])
        {
            [LTools showMBProgressWithText:tostAry[i] addToView:self.view];
            return;
        }
    }
    WEAKSELF;
    NSDictionary *dic = @{@"truckTypeId":_carTypeID,
                          @"carNumber":_carNumStr,
                          @"length":_carLenthStr,
                          @"width":_carWidthStr,
                          @"high":_carHeightStr,
                          @"bearLimit":_carWeightStr,
                          @"street.id":s_str(_streeID),
                          @"parkStation":_carCoordinStr,
                          @"lat":_latitude,
                          @"lng":_lontude,
                          @"driverCardImg":_DriverImgUrl,
                          @"drivingCardImg":_VehicleImgUrl,
                          @"imgs":[NSString stringWithFormat:@"%@,%@,%@",_HeadImgUrl,_BodyImgUrl,_FootImgUrl]
                          };
    [HYBaseHandler requestWithTheFormUrl:SERVER_carSave Parmers:dic Success:^(id obj)
     {
         if ([obj server_Getcodecheck])
         {
             [LTools showMBProgressWithText:@"提交成功，等待审核" addToView:sharedDelegate.window];
             [self.navigationController popViewControllerAnimated:YES];
         }
         else
         {
             [LTools showMBProgressWithText:obj[@"msg"] addToView:weakSelf.view];
         }
     } failed:^(NSError *error) {
         
     }];
    
}

- (void)pushAction:(UIButton *)btn
{
    [self.view endEditing:YES];
    if (btn.tag == 300)
    {
        _imgType = 0;
    }
    else if (btn.tag == 301)
    {
        _imgType = 1;
    }
    else if (btn.tag == 400)
    {
        _imgType = 2;
    }
    else if (btn.tag == 401)
    {
        _imgType = 3;
    }
    else if (btn.tag == 402)
    {
        _imgType = 4;
    }
    
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"选择图片" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    //拍照
    WEAKSELF;
    [alertVC addAction:[UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        HYLog(@"拍照");
        [weakSelf takePhoto];
        
    }]];
    
    //从相册中选取
    [alertVC addAction:[UIAlertAction actionWithTitle:@"从相册中选取" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        HYLog(@"从相册中选取");
        [weakSelf choseImageWithTypeCameraTypePhotoLibrary:UIImagePickerControllerSourceTypePhotoLibrary];
        
    }]];
    [alertVC addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        HYLog(@"取消");
        [weakSelf dismissViewControllerAnimated:NO completion:nil];
    }]];
    
    [self presentViewController:alertVC animated:YES completion:nil];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.view endEditing:YES];
    WEAKSELF;
    if (indexPath.section == 0)
    {
        if (indexPath.row == 0)
        {
            _isLenth = NO;
            [self companyAction];
            [pickTypeView reloadAllComponents];
        }
        else if (indexPath.row == 2)
        {
            if ([_carTypeStr isEqualToString:@""])
            {
                [LTools showMBProgressWithText:@"请先选择货车类型" addToView:self.view];
                return;
            }
            _isLenth = YES;
            [self companyAction];
            [pickTypeView reloadAllComponents];
        }
        else if (indexPath.row == 4)
        {
            CloseGoodsAddressViewController *vc = [[CloseGoodsAddressViewController alloc] init];
            vc.areaType = indexPath.row;
            vc.isPush = YES;
            vc.closeBlock = ^(NSString *name, NSInteger index)
            {
                NSArray *idAry = [name componentsSeparatedByString:@","];
                dispatch_async(dispatch_get_main_queue(), ^{
                    if (idAry.count == 2)
                    {
                        _streeID = idAry[1];
                        _carPlaceStr = idAry[0];
                       
                    }
                    [weakSelf.tableview reloadData];
                });
            };
            [self.navigationController pushViewController:vc animated:YES];
        }
        else if (indexPath.row == 5)
        {
            LogisticsLocationViewController *locationVC = [[LogisticsLocationViewController alloc] init];
            locationVC.locationBlock = ^(NSString *lat, NSString *lng, NSString *address)
            {
                _latitude = lat;
                _lontude = lng;
                _carCoordinStr = address;
                [weakSelf.tableview reloadData];
            };
            [self.navigationController pushViewController:locationVC animated:YES];
        }
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
    {
        return 55*displaywidth;
    }
    else if (indexPath.section == 1)
    {
        return 180*displaywidth;
    }
    else if (indexPath.section == 2)
    {
        return 150*displaywidth;
    }
    return 135*displaywidth;
}

-(void)takePhoto
{
    //    判断是否打开相机权限
    AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
    if (authStatus == AVAuthorizationStatusRestricted || authStatus ==AVAuthorizationStatusDenied)
    {
        UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"您尚未开启相机权限，请去系统设置界面打开相机权限！" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
            if ([[UIApplication sharedApplication]canOpenURL:url]) {
                [[UIApplication sharedApplication]openURL:url];
            }
        }];
        [alertVC addAction:okAction];
        [self presentViewController:alertVC animated:YES completion:nil];
        
        
    }
    else
    {
        UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
        if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera])
        {
            UIImagePickerController *picker = [[UIImagePickerController alloc] init];
            picker.delegate = self;
            //设置拍照后的图片可被编辑
            picker.allowsEditing = YES;
            picker.sourceType = sourceType;
            [self presentViewController:picker animated:YES completion:nil];
        }else
        {
            
        }
        
    }
}


- (void)choseImageWithTypeCameraTypePhotoLibrary:(UIImagePickerControllerSourceType)type{
    
    UIImagePickerController * imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.delegate =self;
    imagePicker.sourceType = type;
    imagePicker.allowsEditing = YES;
    imagePicker.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    imagePicker.allowsEditing =YES;
    [self presentViewController:imagePicker animated:YES completion:^{
        
    }];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage* image = [info objectForKey:@"UIImagePickerControllerEditedImage"];
    WEAKSELF;
    [HttpTool uploadImageWithPath:@"/file/upload" params:nil thumbName:@"file" image:image success:^(id json) {
        if ([json server_Getcodecheck])
        {
            NSArray *imgAry = json[@"data"];
            if (imgAry.count > 0)
            {
                NSString *imgStr = imgAry[0];
                if (weakSelf.imgType == 0)
                {
                    weakSelf.DriverImgUrl = imgStr;
                }
                else if (weakSelf.imgType == 1)
                {
                    weakSelf.VehicleImgUrl = imgStr;
                }
                else if (weakSelf.imgType == 2)
                {
                    weakSelf.HeadImgUrl = imgStr;
                }
                else if (weakSelf.imgType == 3)
                {
                    weakSelf.BodyImgUrl = imgStr;
                }
                else if (weakSelf.imgType == 4)
                {
                    weakSelf.FootImgUrl = imgStr;
                }
                dispatch_async(dispatch_get_main_queue(), ^{
                    [weakSelf.tableview reloadData];
                });
            }
        }
    } failure:^(NSError *error) {
        
    } progress:^(CGFloat progress) {
        
    }];
    [picker dismissViewControllerAnimated:YES completion:^{
        
    }];
}


- (void)requestTruckType
{
    WEAKSELF;
    [HYBaseHandler requestWithTheFormUrl:SERVER_truckType Parmers:nil Success:^(id obj)
     {
         if ([obj server_Getcodecheck])
         {
             _allTypeAry = [TruckTypeModel mj_objectArrayWithKeyValuesArray:obj[@"data"]];
             dispatch_async(dispatch_get_main_queue(), ^{
                 
                 [weakSelf.tableview reloadData];
             });
         }
         else
         {
             [LTools showMBProgressWithText:obj[@"msg"] addToView:weakSelf.view];
         }
     } failed:^(NSError *error) {
         
     }];
}

- (void)requestTruckVerifyInfo
{
    WEAKSELF;
    [HYBaseHandler requestWithTheFormUrl:SERVER_verifyInfo Parmers:nil Success:^(id obj)
     {
         if ([obj[@"data"] isKindOfClass:[NSString class]])
         {
             return;
         }
         if ([obj server_Getcodecheck])
         {
             _carModel = [CarDetailModel mj_objectWithKeyValues:obj[@"data"]];
             NSString *tostStr = @"";
             if ([_carModel.status integerValue] == 0)
             {
                 tostStr = @"货车资料审核中";
             }
             else if ([_carModel.status integerValue] == 1)
             {
                 tostStr = @"货车资料已审核通过，重新填写需重新审核";
             }
             else if ([_carModel.status integerValue] == 2)
             {
                 tostStr = @"货车资料未能通过，请重新编辑后提交";
             }
             UIAlertController *alerVc = [UIAlertController alertControllerWithTitle:@"提示" message:tostStr preferredStyle:UIAlertControllerStyleAlert];
             [alerVc addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                 if ([weakSelf.carModel.status integerValue] == 0)
                 {
                     [weakSelf.navigationController popViewControllerAnimated:YES];
                 }
             }]];
             [self presentViewController:alerVc animated:YES completion:nil];
             dispatch_async(dispatch_get_main_queue(), ^{
                 [weakSelf reloadALLname];
                 [weakSelf.tableview reloadData];
             });
         }
         else
         {
             [LTools showMBProgressWithText:obj[@"msg"] addToView:weakSelf.view];
         }
     } failed:^(NSError *error) {
         
     }];
}

- (void)reloadALLname
{
    //NSArray *nameARRAY = @[_carTypeStr,_carNumStr,_carLenthStr,_carWidthStr,_carHeightStr,_carWeightStr,_carPlaceStr,_carCoordinStr,_DriverImgUrl,_VehicleImgUrl,_HeadImgUrl,_BodyImgUrl,_FootImgUrl];
    _carTypeStr = _carModel.truckType;
    _carNumStr = _carModel.carNumber;
    _carLenthStr = _carModel.length;
    _carWidthStr = _carModel.width;
    _carHeightStr = _carModel.high;
    _carWeightStr = _carModel.bearLimit;
    _carPlaceStr = [NSString stringWithFormat:@"%@%@%@%@",_carModel.street.province,_carModel.street.city,_carModel.street.district,_carModel.street.name];
    _carCoordinStr = _carModel.parkStation;
    _DriverImgUrl = _carModel.driverCardImg;
    _VehicleImgUrl = _carModel.drivingCardImg;
    NSArray *imgAry = [_carModel.imgs componentsSeparatedByString:@","];
    if (imgAry.count == 3)
    {
        _HeadImgUrl = imgAry[0];
        _BodyImgUrl = imgAry[1];
        _FootImgUrl = imgAry[2];
    }
    _carTypeID = _carModel.truckTypeId;
    _streeID = _carModel.street.ID;
    _latitude = _carModel.lat;
    _lontude = _carModel.lng;
    if (_allTypeAry.count > 0)
    {
        for (int i = 0; i < _allTypeAry.count; i++)
        {
            TruckTypeModel *model = _allTypeAry[i];
            if ([_carTypeID integerValue] == [model.ID integerValue])
            {
                _TruckType = i+1;
                break;
            }
        }
    }
    
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    if (textField.tag == 201)
    {
        _carNumStr = textField.text;
    }
    else if (textField.tag == 230)
    {
        _carWidthStr = textField.text;
    }
    else if (textField.tag == 231)
    {
        _carHeightStr = textField.text;
    }
    else if (textField.tag == 232)
    {
        _carWeightStr = textField.text;
    }
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
    
}

- (NSIndexPath *)tableView:(UITableView *)tableView willDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.view endEditing:YES];
    return indexPath;
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self.view endEditing:YES];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
